package com.github.phoenics.militia.bazookaFx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.phoenics.militia.bazookaFx.dao.TableColumnDao;
import com.github.phoenics.militia.bazookaFx.domain.entity.SchemaTableColumnView;
import com.github.phoenics.militia.bazookaFx.domain.entity.TableColumn;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Phoenics Chow
 * on 2019年02月01日
 */
@Service
public class TableColumnService extends ServiceImpl<TableColumnDao, TableColumn> {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TableColumnService.class);
    @Autowired
    SchemaTableColumnViewService schemaTableColumnViewService;
    public void writeColumns(Map<String,Long> idmap, String schemaName){
        QueryWrapper<SchemaTableColumnView> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("table_schema",schemaName);
        schemaTableColumnViewService.list(queryWrapper).forEach(colu->{
            TableColumn tableColumn=new TableColumn();
            BeanUtils.copyProperties(colu,tableColumn);
            tableColumn.setNullable(colu.getIsNullable().equals("YES"));
            String ckey=colu.getColumnKey();
            tableColumn.setColumnKey(ckey!=null && ckey.equals("PRI"));
            String tableName=colu.getTableName();
            tableColumn.setTableId(idmap.get(tableName));
            tableColumn.setColumnCnName(colu.getColumnComment());


        });

    }
}
